<?php
/**
 * Service of Promotion
 *
 * @author fsqijy
 */
require_once BaseUtil::getModulePath("base")  .  '/services/BaseService.php';
require_once BaseUtil::getModulePath("dbgrid") . '/DBGrid.php';
require_once BaseUtil::getModulePath("promotion")  .  '/constants/PromotionConstant.php';
require_once BaseUtil::getModulePath("promotion")  .  '/models/PromotionDao.php';
require_once BaseUtil::getModulePath("promotion")  .  '/models/PromotionConditionDao.php';
require_once BaseUtil::getModulePath("promotion")  .  '/models/PromotionActionDao.php';
require_once BaseUtil::getModulePath("promotion")  .  '/models/SalePricePromotionDao.php';
require_once BaseUtil::getModulePath("inventory")  .  '/models/ProductItemDao.php';
require_once BaseUtil::getModulePath("customer")  .  '/models/CustomerDao.php';
require_once BaseUtil::getModulePath("promotion")  .  '/models/SpecialDao.php';
require_once BaseUtil::getModulePath("promotion")  .  '/models/SpecialNameDao.php';

class PromotionService extends BaseService {

    private $promotionDao;

    private $promotionConditionDao;

    private $promotionActionDao;

    private $productItemDao;

    private $salePricePromotionDao;
    
    private $customerDao;

    private $SpecialProductDao;
    
    private $SpecialNameProductDao;
    
    public function __construct () {
        parent::__construct();
        $this->promotionDao = new PromotionDao();
        $this->promotionDao->setDb($this->db);
        $this->promotionConditionDao = new PromotionConditionDao();
        $this->promotionConditionDao->setDb($this->db);
        $this->promotionActionDao = new PromotionActionDao();
        $this->promotionActionDao->setDb($this->db);
        $this->productItemDao = new ProductItemDao();
        $this->productItemDao->setDb($this->db);
        $this->salePricePromotionDao = new SalePricePromotionDao();
        $this->salePricePromotionDao->setDb($this->db);
        $this->customerDao = new CustomerDao();
        $this->customerDao->setDb($this->db);
        
        $this->specialDao = new SpecialDao(); //dmgy
        $this->specialDao->setDb($this->db);
        $this->specialNameDao = new SpecialNameDao(); 
        $this->specialNameDao->setDb($this->db);                  
    }
    /**
     * get free shipping promotion
     */
    public function getFreeShippingPromotion() {
        $promotion = $this->promotionDao->getFreeShippingPromotion(PromotionConstant::$freeShippingPromotionType);
        return $promotion;
    }

    /**
     * update free shipping promotion
     */
    public function updateFreeShippingPromotion($promotion,$condition) {
        $this->promotionDao->updateFreeShippingPromotion($promotion);
        $this->promotionConditionDao->updateFreeShippingPromotion($condition);
    }

    public function saveProductPromotion($form,$post){
        //save base information
        $selectRestriction = HttpUtil::getPostString('selectRestriction');
        $promotion = $this->getBaseInfo($form, $post, $selectRestriction);
        $promotionId = $this->promotionDao->insertData($promotion);
  
        //save condition
        //save product items
        $productItemIdStr = $post['productItemId'];
        if(!empty($productItemIdStr)){
            $productItemIds = explode(",", $productItemIdStr);
            foreach($productItemIds as $productItemId){
                $condition = array();
                $condition['promotion_id'] = $promotionId;
                $condition['promotion_condition_type'] = 1;
                $condition['condition_value'] = $productItemId;
                $this->promotionConditionDao->insertData($condition);
            }
        }
        //save category
        $categoryIdStr = $post['categoryId'];
        if(!empty($categoryIdStr)){
            $categoryIds = explode(",", $categoryIdStr);
            foreach($categoryIds as $categoryId){
                if($categoryId > 0){
                    $condition = array();
                    $condition['promotion_id'] = $promotionId;
                    $condition['promotion_condition_type'] = 2;
                    $condition['condition_value'] = $categoryId;
                    $this->promotionConditionDao->insertData($condition);
                }
            }
        }
        
      //save customers
      $customerRestriction = HttpUtil::getPostString('customerRestriction');
      if($customerRestriction == 1){
        $customerIds = $post['customerIds'];
        if(!empty($customerIds)){
           foreach($customerIds as $customerId){
                $condition = array();
                $condition['promotion_id'] = $promotionId;
                $condition['promotion_condition_type'] = 6;
                $condition['condition_value'] = $customerId;
                $this->promotionConditionDao->insertData($condition);
            }
        }
      }
        
        //save Code
        $codeNums = HttpUtil::getPostString('select_code_num');
        $codeArr = array();
        if(!empty($codeNums)){
            $codeNumArr = explode(",",$codeNums);
            
            foreach ($codeNumArr as $codeNum){
        	    $code = HttpUtil::getPostString('code_'.$codeNum);
        	    if(!empty($code) && !array_key_exists($code,$codeArr)){
        	         $condition = array();
                     $condition['promotion_id'] = $promotionId;
                     $condition['promotion_condition_type'] = 7;
                     $condition['code_value'] = $code;
                     $condition['condition_value'] = HttpUtil::getPostString('customerid_'.$codeNum);
                     $this->promotionConditionDao->insertData($condition);
        	        $codeArr[$code] = $code;
        	    }
            }
         
        }
      
      //Save customer cerft and rand condition  
        $this->saveCerftAndRankCondition($promotionId);
        
        //save action
        $actionTypes = $post['pro_action_type'];
        $actionValues = $post['pro_action_value'];
        if(!empty($actionTypes)){
            $i = 0;
            foreach($actionTypes as $actionType){
                $action = array();
                $action['promotion_id'] = $promotionId;
                $action['promotion_action_type'] = $actionType;
                $action['action_value'] = $actionValues[$i];
                $this->promotionActionDao->insertData($action);
                $i++;
            }
        }
    }

    public function updateProductPromotion($form,$post){
        //save base information
        $promotion = $this->getBaseInfo($form, $post);
        $promotionId = $post['promotionid'];
        $this->promotionDao->updatePromotion($promotionId, $promotion);
        //save condition
        //delete condition
        $this->promotionConditionDao->delCondition($promotionId);
        //save product items
        $productItemIdStr = $post['productItemId'];
        if(!empty($productItemIdStr)){
            $productItemIds = explode(",", $productItemIdStr);
            foreach($productItemIds as $productItemId){
                $condition = array();
                $condition['promotion_id'] = $promotionId;
                $condition['promotion_condition_type'] = 1;
                $condition['condition_value'] = $productItemId;
                $this->promotionConditionDao->insertData($condition);
            }
        }
        //save category
        $categoryIdStr = $post['categoryId'];
        if(!empty($categoryIdStr)){
            $categoryIds = explode(",", $categoryIdStr);
            foreach($categoryIds as $categoryId){
                if($categoryId > 0){
                    $condition = array();
                    $condition['promotion_id'] = $promotionId;
                    $condition['promotion_condition_type'] = 2;
                    $condition['condition_value'] = $categoryId;
                    $this->promotionConditionDao->insertData($condition);
                }
            }
        }
        
     //save customers
      $customerRestriction = HttpUtil::getPostString('customerRestriction');
      if($customerRestriction == 1){
        $customerIds = $post['customerIds'];
        if(!empty($customerIds)){
           foreach($customerIds as $customerId){
                $condition = array();
                $condition['promotion_id'] = $promotionId;
                $condition['promotion_condition_type'] = 6;
                $condition['condition_value'] = $customerId;
                $this->promotionConditionDao->insertData($condition);
            }
        }
      }
        
        //save code restriction
        $codeNums = HttpUtil::getPostString('select_code_num');
        if(!empty($codeNums)){
            $codeNumArr = explode(",",$codeNums);
           
            $codeArr = array();
            foreach ($codeNumArr as $codeNum){
        	    $code = HttpUtil::getPostString('code_'.$codeNum);
                if(!empty($code) && !array_key_exists($code,$codeArr)){
        	        $condition = array();
                    $condition['promotion_id'] = $promotionId;
                    $condition['promotion_condition_type'] = 7;
                    $condition['code_value'] = $code;
                    $condition['condition_value'] = HttpUtil::getPostString('customerid_'.$codeNum);
                    $this->promotionConditionDao->insertData($condition);
        	        $codeArr[$code] = $code;
        	
        	    }
            }
            
            
        }
        
       //Save customer cerft and rand condition  
        $this->saveCerftAndRankCondition($promotionId);
        
        
        //save action
        $actionTypes = $post['pro_action_type'];
        $actionValues = $post['pro_action_value'];
        if(!empty($actionTypes)){
            //delete action
            $this->promotionActionDao->delAction($promotionId);
            //insert action
            $i = 0;
            foreach($actionTypes as $actionType){
                $action = array();
                $action['promotion_id'] = $promotionId;
                $action['promotion_action_type'] = $actionType;
                $action['action_value'] = $actionValues[$i];
                $this->promotionActionDao->insertData($action);
                $i++;
            }
        }
    }

    public function saveOrderPromotion($form,$post){
        //save base information
        $promotion = $this->getBaseInfo($form, $post);
        $promotionId = $this->promotionDao->insertData($promotion);
        //save condition
        $selectedRestrictionType = $post['selectedRestrictionType'];
        //subtotal Restriction
        if($selectedRestrictionType == 3){
            $operator = $post['operator'];
            $total = trim($post['total']);
            $condition = array();
            $condition['promotion_id'] = $promotionId;
            $condition['promotion_condition_type'] = 3;
            $condition['condition_value'] = $operator;
            $condition['condition_second_value'] = $total;
            $this->promotionConditionDao->insertData($condition);
        }
        
    //save customers
      $customerRestriction = HttpUtil::getPostString('customerRestriction');
      if($customerRestriction == 1){
        $customerIds = $post['customerIds'];
        if(!empty($customerIds)){
           foreach($customerIds as $customerId){
                $condition = array();
                $condition['promotion_id'] = $promotionId;
                $condition['promotion_condition_type'] = 6;
                $condition['condition_value'] = $customerId;
                $this->promotionConditionDao->insertData($condition);
            }
        }
      }
     //save code restriction
        $codeNums = HttpUtil::getPostString('select_code_num');
        if(!empty($codeNums)){
            $codeNumArr = explode(",",$codeNums);
            
            $codeArr = array();
            foreach ($codeNumArr as $codeNum){
        	    $code = HttpUtil::getPostString('code_'.$codeNum);
                if(!empty($code) && !array_key_exists($code,$codeArr)){
        	        $condition = array();
                    $condition['promotion_id'] = $promotionId;
                    $condition['promotion_condition_type'] = 7;
                    $condition['code_value'] = $code;
                    $condition['condition_value'] = HttpUtil::getPostString('customerid_'.$codeNum);
                    $this->promotionConditionDao->insertData($condition);
        	        $codeArr[$code] = $code;
        	    }
            }
         }
         
       //Save customer cerft and rand condition  
        $this->saveCerftAndRankCondition($promotionId);
        
        //save action
        $actionTypes = $post['pro_action_type'];
        $actionValues = $post['pro_action_value'];
        if(!empty($actionTypes)){
            $i = 0;
            foreach($actionTypes as $actionType){
                $action = array();
                $action['promotion_id'] = $promotionId;
                $action['promotion_action_type'] = $actionType;
                $action['action_value'] = $actionValues[$i];
                $this->promotionActionDao->insertData($action);
                $i++;
            }
        }
    }

    public function updateOrderPromotion($form,$post){
        //save base information
        $promotion = $this->getBaseInfo($form, $post);
        $promotionId = $post['promotionid'];
        $this->promotionDao->updatePromotion($promotionId, $promotion);
        //save condition
        //delete condition
        $this->promotionConditionDao->delCondition($promotionId);
        //insert new condition
        $selectedRestrictionType = $post['selectedRestrictionType'];
        //subtotal Restriction
        if($selectedRestrictionType == 3){
            $operator = $post['operator'];
            $total = trim($post['total']);
            $condition = array();
            $condition['promotion_id'] = $promotionId;
            $condition['promotion_condition_type'] = 3;
            $condition['condition_value'] = $operator;
            $condition['condition_second_value'] = $total;
            $this->promotionConditionDao->insertData($condition);
        }
        
    //save customers
      $customerRestriction = HttpUtil::getPostString('customerRestriction');
      if($customerRestriction == 1){
        $customerIds = $post['customerIds'];
        if(!empty($customerIds)){
           foreach($customerIds as $customerId){
                $condition = array();
                $condition['promotion_id'] = $promotionId;
                $condition['promotion_condition_type'] = 6;
                $condition['condition_value'] = $customerId;
                $this->promotionConditionDao->insertData($condition);
            }
        }
      }
        
        //save code restriction
        $codeNums = HttpUtil::getPostString('select_code_num');
        if(!empty($codeNums)){
            $codeNumArr = explode(",",$codeNums);
           
            $codeArr = array();
            foreach ($codeNumArr as $codeNum){
        	    $code = HttpUtil::getPostString('code_'.$codeNum);
                if(!empty($code) && !array_key_exists($code,$codeArr)){
        	         $condition = array();
                     $condition['promotion_id'] = $promotionId;
                     $condition['promotion_condition_type'] = 7;
                     $condition['code_value'] = $code;
                     $condition['condition_value'] = HttpUtil::getPostString('customerid_'.$codeNum);
                     $this->promotionConditionDao->insertData($condition);
        	         $codeArr[$code] = $code;
        	
        	    }
            }
           
        }
        
     //Save customer cerft and rand condition  
        $this->saveCerftAndRankCondition($promotionId);   
        
        //save action
        $actionTypes = $post['pro_action_type'];
        $actionValues = $post['pro_action_value'];
        if(!empty($actionTypes)){
            //delete action
            $this->promotionActionDao->delAction($promotionId);
            //insert action
            $i = 0;
            foreach($actionTypes as $actionType){
                $action = array();
                $action['promotion_id'] = $promotionId;
                $action['promotion_action_type'] = $actionType;
                $action['action_value'] = $actionValues[$i];
                $this->promotionActionDao->insertData($action);
                $i++;
            }
        }
    }

    public function saveSalePromotion($form,$post){
        //save base information
        $promotion = $this->getBaseInfo($form, $post);
        $promotionId = $this->promotionDao->insertData($promotion);
       
        
       //save customers
      $customerRestriction = HttpUtil::getPostString('customerRestriction');
      if($customerRestriction == 1){
        $customerIds = $post['customerIds'];
        if(!empty($customerIds)){
           foreach($customerIds as $customerId){
                $condition = array();
                $condition['promotion_id'] = $promotionId;
                $condition['promotion_condition_type'] = 6;
                $condition['condition_value'] = $customerId;
                $this->promotionConditionDao->insertData($condition);
            }
        }
      }
        //save code restriction
        $codeNums = HttpUtil::getPostString('select_code_num');
        if(!empty($codeNums)){
            $codeNumArr = explode(",",$codeNums);
            
            $codeArr = array();
            foreach ($codeNumArr as $codeNum){
        	    $code = HttpUtil::getPostString('code_'.$codeNum);
                if(!empty($code) && !array_key_exists($code,$codeArr)){
        	        $condition = array();
                    $condition['promotion_id'] = $promotionId;
                    $condition['promotion_condition_type'] = 7;
                    $condition['code_value'] = $code;
                    $condition['condition_value'] = HttpUtil::getPostString('customerid_'.$codeNum);
                    $this->promotionConditionDao->insertData($condition);
        	        $codeArr[$code] = $code;
        	
        	    }
            }
            
            
        }

      //Save customer cerft and rand condition  
        $this->saveCerftAndRankCondition($promotionId);
        
        //save action
        $actionTypes = $post['pro_action_type'];
        $actionValues = $post['pro_action_value'];
        if(!empty($actionTypes)){
            $i = 0;
            foreach($actionTypes as $actionType){
                $action = array();
                $action['promotion_id'] = $promotionId;
                $action['promotion_action_type'] = $actionType;
                $action['action_value'] = $actionValues[$i];
                $this->promotionActionDao->insertData($action);
                $i++;
            }
        }
    }

    public function updateSalePromotion($form,$post){
        //save base information
        $promotion = $this->getBaseInfo($form, $post);
        $promotionId = $post['promotionid'];
        $this->promotionDao->updatePromotion($promotionId, $promotion);
        
        //save condition
        //delete condition
        $this->promotionConditionDao->delCondition($promotionId);
        
       //save customers
      $customerRestriction = HttpUtil::getPostString('customerRestriction');
      if($customerRestriction == 1){
        $customerIds = $post['customerIds'];
        if(!empty($customerIds)){
           foreach($customerIds as $customerId){
                $condition = array();
                $condition['promotion_id'] = $promotionId;
                $condition['promotion_condition_type'] = 6;
                $condition['condition_value'] = $customerId;
                $this->promotionConditionDao->insertData($condition);
            }
        }
      }
        //save code restriction
        $codeNums = HttpUtil::getPostString('select_code_num');
        if(!empty($codeNums)){
            $codeNumArr = explode(",",$codeNums);
            
            $codeArr = array();
            foreach ($codeNumArr as $codeNum){
        	    $code = HttpUtil::getPostString('code_'.$codeNum);
                if(!empty($code) && !array_key_exists($code,$codeArr)){
        	        $condition = array();
                    $condition['promotion_id'] = $promotionId;
                    $condition['promotion_condition_type'] = 7;
                    $condition['code_value'] = $code;
                    $condition['condition_value'] = HttpUtil::getPostString('customerid_'.$codeNum);
                    $this->promotionConditionDao->insertData($condition);
        	        $codeArr[$code] = $code;
        	
        	    }
            }
          
        }
        
      //Save customer cerft and rand condition  
        $this->saveCerftAndRankCondition($promotionId);
        
        //save action
        $actionTypes = $post['pro_action_type'];
        $actionValues = $post['pro_action_value'];
        if(!empty($actionTypes)){
            //delete action
            $this->promotionActionDao->delAction($promotionId);
            //insert action
            $i = 0;
            foreach($actionTypes as $actionType){
                $action = array();
                $action['promotion_id'] = $promotionId;
                $action['promotion_action_type'] = $actionType;
                $action['action_value'] = $actionValues[$i];
                $this->promotionActionDao->insertData($action);
                $i++;
            }
        }
    }

    private function getBaseInfo($form,$post,$restriction = 0){
        $promotion = array();
        
        $name = $form->getValue("name");
        $show_in_product = $form->getValue("show_in_product");
        $short_description = $form->getValue("short_description");
        $long_description = $form->getValue("long_description");
        
      //  $code = $form->getValue("code");
        $uses = $form->getValue("uses");
        
        $from = $form->getValue("from");
        $to = $form->getValue("to");
        $status = $form->getValue("status");
        
        $stackable = $form->getValue("stackable");
        
        //save Promotion
        $promotion["name"] = $name;
      //  $promotion["code"] = $code;
        $promotion['uses_per_customer'] = $form->getValue("uses_per_customer");
        $promotion["total_uses"] = $uses;
        
        $promotion["show_in_product"] = $show_in_product;
        $promotion["short_description"] = $short_description;
        $promotion["long_description"] = $long_description;
        $promotion["date_from"] = $from.' '.$post['fhour'].':'.$post['fmin'].':'.$post['fsec'];
        $promotion["date_to"] = $to.' '.$post['thour'].':'.$post['tmin'].':'.$post['tsec'];
        $promotion["status"] = $status;
        $promotion["auto_apply"] = $form->getValue("auto_apply");
        $promotion["promotion_type"] = $post['promotiontype'];
        
        $promotion["stackable"] = $stackable;
        $promotion["priority"] = $form->getValue("priority");
        $isExchangePoint =HttpUtil::getPostString('point_exchange_select');
        $promotion["is_exchange_point"] = $isExchangePoint;
        if($isExchangePoint == 1){
        	$promotion["exhaust_points"] = $form->getValue('exhaust_point');
        	$promotion["min_ac_points"] = $form->getValue('min_ac_point');
        	$promotion["exchange_start_date"] = $form->getValue('start_date');
        	$promotion["exchange_end_date"] = $form->getValue('end_date');
        } else {
        	$promotion["exhaust_points"] = '';
        	$promotion["min_ac_points"] = '';
        	$promotion["exchange_start_date"] = '';
        	$promotion["exchange_end_date"] = '';
        }
        return $promotion;
    }
    
    public function getPromotionByid($promotionid) {
        return $this->promotionDao->getPromotionByid($promotionid);
    }
    public function getConditionBypromotionid($promotionid) {
        return $this->promotionConditionDao->getConditionBypromotionid($promotionid);
    }
    public function getConditionByType($promotionid,$type) {
        return $this->promotionConditionDao->getConditionByType($promotionid,$type);
    }
    public function getActionBypromotionid($promotionid) {
        return $this->promotionActionDao->getActionBypromotionid($promotionid);
    }
    public function updatePromotion($promotionid,$promotion) {
        return $this->promotionDao->updatePromotion($promotionid,$promotion);
    }
    public function delCondition($promotionid) {
        return $this->promotionConditionDao->delCondition($promotionid);
    }
    public function delAction($promotionid) {
        return $this->promotionActionDao->delAction($promotionid);
    }
    public function deletePromotion($promotionid) {
        $this->promotionDao->deletePromotion($promotionid);
        $this->promotionConditionDao->deleteCondition($promotionid);
        $this->promotionActionDao->deleteAction($promotionid);
    }
    public function deleteCondition($promotionid) {
        return $this->promotionConditionDao->deleteCondition($promotionid);
    }
    public function deleteAction($promotionid) {
        return $this->promotionActionDao->deleteAction($promotionid);
    }
    public function getPromotionByName($name) {
        return $this->promotionDao->getPromotionByName($name);
    }
    public function getPromotionByCode($name) {
        return $this->promotionDao->getPromotionByCode($name);
    }

    public function getSalePriceGrid($view,$form) {
        $dbGrid = new DBGrid($view);
        $dbGrid->setPost($form);
        $dbGrid->setModule("/promotion/promotion");
        $dbGrid->setSortOrder("sale_price_promotions-created", "DESC");
        $dbGrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "products-title",$this->_translate->translate("productTitle"));
        $dbGrid->addDateFilter("sale_price_promotions-created", $this->_translate->translate("createDate"));
        $dbGrid->addValueFilter("sale_price_promotions-is_delete",0);
        $dbGrid->addFetchTable("sale_price_promotions","*");
        $dbGrid->addFetchTable("product_items",array("id as product_item_id","upc","sku","list_price"),"sale_price_promotions.product_item_id = product_items.id");
        $dbGrid->addFetchTable("products",array("title"),"product_items.product_id = products.id");
        $dbGrid->addFetchColumn("title",$this->_translate->translate("productTitle"),"editsaleprice");
        //$dbGrid->addFetchColumn("title",$this->_translate->translate("productTitle"));
        $dbGrid->addFetchColumn("upc",$this->_translate->translate("upc"));
        $dbGrid->addFetchColumn("sku",$this->_translate->translate("itemSku"));
        $dbGrid->addFetchColumn("sale_price",$this->_translate->translate("salePrice"));
        $dbGrid->addFetchColumn("list_price",$this->_translate->translate("listPrice"));
        $dbGrid->addFetchColumn("date_from",$this->_translate->translate("from"));
        $dbGrid->addFetchColumn("date_to",$this->_translate->translate("to"));
        $dbGrid->addFetchColumn("created",$this->_translate->translate("createDate"));
        $dbGrid->setPageTitle($this->_translate->translate("salePriceTitle"));
        if(AAService::isAllowed("promotion", "addsaleprice")) {
            $dbGrid->addModuleAction("add",$this->_translate->translate("addBtn"),"addsaleprice");
        }
        return $dbGrid->render();
    }

    public function addSalePrice($form,$post) {
        $action = $form->getValue("action");
        $from = $form->getValue("from");
        $to = $form->getValue("to");
        $discount = $form->getValue("amount");
        $items = $post["itemIds"];
        $itemId = implode(",", $items);
        if(!empty($items)) {
            $listPrices = $this->productItemDao->getListPriceByIds($itemId);
        }
        if(!empty($listPrices)) {
            foreach($items as $item) {
                $listPrice = $listPrices[$item];
                $salePrice = 0 ;
                if($action == 1) {
                    $salePrice = $listPrice - $listPrice * $discount * 0.01;
                }else if($action == 2) {
                        $salePrice = $listPrice - $discount;
                    }
                if($salePrice > 0) {
                    $data = array();
                    $data['product_item_id'] = $item;
                    $data['sale_price'] =  number_format($salePrice,2);;
                    $data['date_from'] =  $from;
                    $data['date_to'] =  $to;
                    $this->salePricePromotionDao->insertData($data);
                }
            }
        }
    }

   
    public function getSalePricePromotionById($id) {
        $promotion = $this->salePricePromotionDao->getByCol("id", $id);
        return $promotion;
    }

    public function getProductItemInfo($productItemId) {
        $productItem = $this->productItemDao->getProductItemById($productItemId);
        return $productItem;
    }

    public function updateSalePricePromotion($data,$id) {
        $this->salePricePromotionDao->updateSalePricePromotion($data, $id);
    }

    public function getCouponPromotion($view,$form) {
    	$statusArr = array(1=>$this->_translate->translate('statusA'),0=>$this->_translate->translate('statusI'));
        $dbGrid = new DBGrid($view);
        $dbGrid->setPost($form);
        $dbGrid->setModule("/promotion/promotion");
        $dbGrid->setSortOrder("promotions-created", "DESC");
        $dbGrid->addValueFilter("promotions-is_delete",0);
        $dbGrid->addValueFilter("promotions-promotion_type",1,DBGridConstant::OPERATOR_NOT_EQUAL);
        $dbGrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "promotions-name",$this->_translate->translate("pname"));
        $dbGrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "promotions-code",$this->_translate->translate("code"));
        $dbGrid->addOptionsFilter(DBGridConstant::FILTER_TYPE_SELECT, "promotions-status", $this->_translate->translate("promotionStatus"),array("0"=>$this->_translate->translate("statusI"),"1"=>$this->_translate->translate("statusA")));
        $dbGrid->addDateFilter("promotions-created", $this->_translate->translate("createDate"));
        $dbGrid->addFetchTable("promotions","*");
        $dbGrid->addFetchColumn("name",$this->_translate->translate("pname"),"editcoupon");
        $dbGrid->addFetchColumn("status",$this->_translate->translate("promotionStatus"),"",$statusArr);
        $dbGrid->addFetchColumn("promotion_type",$this->_translate->translate("type"),"",PromotionConstant::getCouponTypeList());
        $dbGrid->addFetchColumn("created",$this->_translate->translate("createDate"));
        $dbGrid->setPageTitle($this->_translate->translate("manageCouponPromotion"));
        if(AAService::isAllowed("promotion", "coupontype")) {
            $dbGrid->addModuleAction("add",$this->_translate->translate("addBtn"),"coupontype");
        }
        return $dbGrid->render();
    }
   
    public function deleteSalePricePromotion($id) {
        $data = array("is_delete"=>1);
        $this->salePricePromotionDao->updateSalePricePromotion($data, $id);
    }

    public function getProductItemByIds($ids){
        $idStr = implode(",", $ids);
        $result = $this->productItemDao->getProductItemByIds($idStr);
        return $result;
    }
    
    public function getAllConditionByType($type,$promotionid = 0){
    	return $this->promotionConditionDao->getAllConditionByType($type,$promotionid);
    }
    
    public function getCustomersByIds($ids){
    	$idStr = implode(",", $ids);
        $result = $this->customerDao->getCustomersByIds($idStr);
        return $result;
    }
    
    private function saveCerftAndRankCondition($promotionId){
     //save customer certf
       $certfs = HttpUtil::getPostString('selectedCustomerCerfts');
       if(!empty($certfs)){
       	   $certfArr = explode(",",$certfs);
       	   foreach($certfArr as $certf){
       	   	   $condition = array();
               $condition['promotion_id'] = $promotionId;
               $condition['promotion_condition_type'] = 8;
               $condition['condition_value'] = $certf;
               $this->promotionConditionDao->insertData($condition);
       	   }
       }
        
       //save customer rank
       $ranks = HttpUtil::getPostString('selectedCustomerRanks');
       if(!empty($ranks)){
       	   $rankArr = explode(",",$ranks);
       	   foreach($rankArr as $rank){
       	   	   $condition = array();
               $condition['promotion_id'] = $promotionId;
               $condition['promotion_condition_type'] = 9;
               $condition['condition_value'] = $rank;
               $this->promotionConditionDao->insertData($condition);
       	   }
       }
    }


    //促销专场标题
    public function getSpecialNamePromotion($view,$form) {
        $dbGrid = new DBGrid($view);
        $dbGrid->setPost($form);
        $dbGrid->setModule("/promotion/promotion");
        $dbGrid->setSortOrder("special_sales-id", "DESC"); 
        $dbGrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "title",$this->_translate->translate("specialName"));
        $dbGrid->addValueFilter("special_sales-is_delete",0);
        $dbGrid->addFetchTable("special_sales","*");
        $dbGrid->addFetchColumn("id",$this->_translate->translate("spProductId"),"editspecialname");
        $dbGrid->addFetchColumn("title",$this->_translate->translate("specialName"));
        $dbGrid->addFetchColumn("created",$this->_translate->translate("createDate"));
        $dbGrid->addFetchColumn("modified",$this->_translate->translate("editDate"));
        $dbGrid->setPageTitle($this->_translate->translate("specialNameTitle"));
        if(AAService::isAllowed("promotion", "specialname")) {
            $dbGrid->addModuleAction("add",$this->_translate->translate("addBtn"),"addspecialname");
        }
        return $dbGrid->render();
    }      

     public function addSpecialName($form,$post) {
        $specialName = $form->getValue("specialName");
        if(!$specialName)
        {
        	return $this->render('addspecialname');
        }
        else
        { 	
            $data = array();
            $data['title'] = $specialName;
            $data['is_delete'] =  0;     
            $this->specialNameDao->insertData($data);
        }
    }    
    
    public function getSpecialNamePromotionById($id) {
        $promotion = $this->specialNameDao->getByCol("id", $id);
        return $promotion;
    } 

    public function updateSpecialNamePromotion($data,$id) {
        $this->specialNameDao->updateSpecialNamePromotion($data, $id);
    }    

    public function deleteSpecialNamePromotion($id) {
        $data = array("is_delete"=>1);
        $this->specialNameDao->updateSpecialNamePromotion($data, $id);
    }      

    //@author dmgy
    public function addSpecial($form,$post) {
        $productCode = $form->getValue("productCode");
        $specialCode = $form->getValue("specialCode");
        $special_sale_id = $form->getValue("special_sale_id");
        $from = $form->getValue("from");
        $to = $form->getValue("to");
            if(!empty($specialCode)){
            $data = array();
            $data['product_item_id'] = $productCode;
            $data['special_type'] =  $specialCode;
            $data['special_sale_id'] =  $special_sale_id;
            $data['is_delete'] =  0;
            $data['modified'] =  $from;
            $data['created'] =  $to;      
            $this->specialDao->insertData($data);
        }
    }    
    
    public function getAllSpecialSales()
    {
    	return $this->specialNameDao->getAllSpecialSales();
    }
    
    
    public function getTextPromotion($view,$form) {
        $dbGrid = new DBGrid($view);
        $dbGrid->setPost($form);
        $dbGrid->setModule("/promotion/promotion");
        $dbGrid->setSortOrder("special_product_items-special_type", "DESC"); //创建促销专场数据
        $dbGrid->setSortOrder("special_product_items-id", "ASC"); //创建促销专场数据
        $dbGrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "special_type",$this->_translate->translate("specialCode"));
        $dbGrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "title",$this->_translate->translate("specialName"));
        //$dbGrid->addDateFilter("sale_price_promotions-created", $this->_translate->translate("createDate"));
        $dbGrid->addValueFilter("special_product_items-is_delete",0);
        $dbGrid->addFetchTable("special_product_items","*");
        $dbGrid->addFetchTable("special_sales", "title","special_product_items.special_sale_id = special_sales.id");
        $dbGrid->addFetchColumn("id",$this->_translate->translate("spProductId"),"editspecial");
        $dbGrid->addFetchColumn("special_type",$this->_translate->translate("specialCode"));
        $dbGrid->addFetchColumn("title",$this->_translate->translate("specialName"));
        $dbGrid->addFetchColumn("product_item_id",$this->_translate->translate("specialProductId"));
        $dbGrid->addFetchColumn("modified",$this->_translate->translate("editDate"));
        $dbGrid->addFetchColumn("created",$this->_translate->translate("createDate"));
        $dbGrid->setPageTitle($this->_translate->translate("specialTitle"));
        if(AAService::isAllowed("promotion", "special")) {
            $dbGrid->addModuleAction("add",$this->_translate->translate("addBtn"),"addspecial");
        }
        return $dbGrid->render();
    }    
    
    public function getSpecialPromotionById($id) {
        $promotion = $this->specialDao->getByCol("id", $id);
        return $promotion;
    } 

    public function updateSpecialPromotion($data,$id) {
        $this->specialDao->updateSpecialPromotion($data, $id);
    }    

    public function deleteSpecialPromotion($id) {
        $data = array("is_delete"=>1);
        $this->specialDao->updateSpecialPromotion($data, $id);
    }        
}